#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    int a[20010][2];
    for (int i = 1; i <= n; i++) {
        if (i == 1)
            a[i][0] = -1, a[i][1] = 2;
        else if (i == n)
            a[i][0] = n - 1, a[i][1] = -1;
        else
            a[i][0] = i - 1, a[i][1] = i + 1;
    }
    int head = 1;
    while (m--) {
        int mode, x, y;
        scanf("%d%d%d", &mode, &x, &y);
        int px = a[x][0], py = a[y][0];
        if (mode == 1 || mode == 2) {
            if (mode == 1 && py == x) continue;
            if (mode == 2 && px == y) continue;

            if (px != -1) {
                a[px][1] = a[x][1];
                if (a[x][1] != -1) a[a[x][1]][0] = px;
            }

            else
                head = a[x][1];

            if (mode == 1) {
                if (py != -1)
                    a[py][1] = x;
                else
                    head = x;
                a[x][1] = y;
                a[x][0] = py;
                a[y][0] = x;
            } else if (mode == 2) {
                if (a[y][1] != -1) a[a[y][1]][0] = x;
                a[x][1] = a[y][1];
                a[x][0] = y;
                a[y][1] = x;
            }
        } else {
            int t0 = a[x][0], t1 = a[x][1];

            a[x][0] = a[y][0] == x ? y : a[y][0];
            a[x][1] = a[y][1] == x ? y : a[y][1];
            a[y][0] = t0 == y ? x : t0;
            a[y][1] = t1 == y ? x : t1;

            if (a[x][0] != -1) a[a[x][0]][1] = x;
            if (a[y][0] != -1) a[a[y][0]][1] = y;
            if (a[x][1] != -1) a[a[x][1]][0] = x;
            if (a[y][1] != -1) a[a[y][1]][0] = y;

            if (head == x)
                head = y;
            else if (head == y)
                head = x;
        }

        // for (int i = head; i != -1; i = a[i][1]) {
        //     printf("%d<-%d->%d\n", a[i][0], i, a[i][1]);
        // }
        // printf("\n");
    }

    int ans = 0, tag = 1;
    for (int i = head; i != -1; i = a[i][1]) {
        ans += i * tag;
        tag = !tag;
    }
    printf("%d\n", ans);
}